﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms.DataVisualization.Charting;
namespace CubePresentation
{
    /// <summary>
    /// Interaction logic for Chart.xaml
    /// </summary>

    public partial class Chart : UserControl
    {
        ChartArea chartArea;
        Legend legend;
        Series series;

        public const SeriesChartType Pie = SeriesChartType.Pie;
        public const SeriesChartType Doughnut = SeriesChartType.Doughnut;
        public const SeriesChartType Bar = SeriesChartType.Bar;
        public const SeriesChartType Line = SeriesChartType.Line;
        public Chart()
        {       
            InitializeComponent();

            chartArea = new ChartArea("chartArea");
            chart.ChartAreas.Add(chartArea);

            legend = new Legend("legend");
            legend.IsTextAutoFit = true;
            chart.Legends.Add(legend);

            series = new Series();
            series.ChartArea = "chartArea";
            series.Legend = "legend";
            series.IsVisibleInLegend = false;
            chart.Series.Add(series);
        }

        public void setChart(string name, string[] fields, double[] values, SeriesChartType type)
        {
            
            chartArea.Area3DStyle.Enable3D = true;
            series.Name = name;
            series.ChartType = type;
            if (type == SeriesChartType.Bar)
            {
                chartArea.AxisX.Interval = 1;
                chartArea.AxisX.TextOrientation = TextOrientation.Auto;
                series.ChartType = SeriesChartType.Column;
            }
            else if (type == SeriesChartType.Pie)
            {
                List<string> tempFields = new List<string>();
                List<double> tempValues = new List<double>();
                for(int i = 0; i < values.Count(); ++i)
                {
                    if (values[i] != 0.0)
                    {
                        tempFields.Add(fields[i]);
                        tempValues.Add(values[i]);
                    }
                    fields = tempFields.ToArray();
                    values = tempValues.ToArray();
                }
            }
            series.Points.DataBindXY(fields, values);
            series.IsVisibleInLegend = true;
        }
    }
}
